//
//  PhotoSelectorViewController.m
//  WisdomCommunity
//
//  Created by Yahaong on 16/7/29.
//  Copyright © 2016年 zonten. All rights reserved.
//

#import "PhotoSelectorViewController.h"
#import "PhotoSelectorCell.h"
#import "YHMarcros.h"

static NSString *const PhotoSelectorCellReuseIdentifier = @"PhotoSelectorCellReuseIdentifier";
@interface PhotoSelectorViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong)UICollectionViewFlowLayout *flowLayou;
@property (nonatomic, strong)UICollectionView *collcetionView;
@property (nonatomic, strong)NSMutableArray *  pictureImages;
@end

@implementation PhotoSelectorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self  setupUI];
}

- (void)setupUI
{
    [self.view addSubview:self.collcetionView];
    [self.collcetionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}


#pragma mark <UICollectionViewDataSource>
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.pictureImages.count + 1;

}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoSelectorCell *cell  = [collectionView dequeueReusableCellWithReuseIdentifier:PhotoSelectorCellReuseIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
    cell.image = (self.pictureImages.count == indexPath.item) ? nil : self.pictureImages[indexPath.item];
    return cell;
}


#pragma mark <懒加载>

- (NSMutableArray *)pictureImages
{
    if (!_pictureImages) {
        _pictureImages = [NSMutableArray array];
    }
    return _pictureImages;
}

- (UICollectionViewFlowLayout *)flowLayou
{
    if (!_flowLayou) {
        _flowLayou = [[UICollectionViewFlowLayout alloc]init];
        _flowLayou.itemSize = CGSizeMake(80, 80);
        _flowLayou.minimumLineSpacing = 10.f;
        _flowLayou.minimumInteritemSpacing=  10.f;
        _flowLayou.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    }
    return _flowLayou;
}
- (UICollectionView *)collcetionView
{
    if (!_collcetionView) {
        _collcetionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayou];
        _collcetionView.delegate = self;
        _collcetionView.dataSource = self;
        _collcetionView.backgroundColor = [UIColor redColor];
        [_collcetionView registerClass:[PhotoSelectorCell class] forCellWithReuseIdentifier:PhotoSelectorCellReuseIdentifier];
        
    }
    return _collcetionView;
}

@end
